package com.hsogoo.game.config;

import com.corundumstudio.socketio.AuthorizationListener;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.HandshakeData;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.SpringAnnotationScanner;
import com.hsogoo.game.server.GameEventDispatcher;
import com.hsogoo.game.server.handler.GameEventHandler;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;

@org.springframework.context.annotation.Configuration
public class GameServerConfiguration {
    @Value("${server.host}")
    private String host;

    @Value("${server.port}")
    private Integer port;

    @Value("${server.threads}")
    private String threads;

    private SocketIOServer server;

//    @Autowired
//    private GameEventDispatcher gameEventDispatcher;

    @Bean
    public SocketIOServer socketIOServer() throws NoSuchAlgorithmException, IOException {
        Configuration config = new Configuration();
        config.setHostname("localhost");
        config.setPort(port);
//		config.getSocketConfig().setReuseAddress(true);
//		config.setSocketConfig(new SocketConfig());
//		config.setOrigin("http://im.uckefu.com");


//		File sslFile = new File(path , "ssl/https.properties") ;
//        if(sslFile.exists()){
//        	Properties sslProperties = new Properties();
//        	FileInputStream in = new FileInputStream(sslFile);
//        	sslProperties.load(in);
//        	in.close();
//        	if(!StringUtils.isBlank(sslProperties.getProperty("key-store")) && !StringUtils.isBlank(sslProperties.getProperty("key-store-password"))){
//        		config.setKeyStorePassword(UKTools.decryption(sslProperties.getProperty("key-store-password")));
//        	    InputStream stream = new FileInputStream(new File(path , "ssl/"+sslProperties.getProperty("key-store")));
//        	    config.setKeyStore(stream);
//        	}
//        }


//	    config.setSSLProtocol("https");
        int workThreads = !StringUtils.isBlank(threads) && threads.matches("[\\d]{1,6}") ? Integer.parseInt(threads) : 100;
        config.setWorkerThreads(workThreads);

//		config.setStoreFactory(new HazelcastStoreFactory());
        config.setAuthorizationListener(new AuthorizationListener() {
            public boolean isAuthorized(HandshakeData data) {
                return true;    //其他安全验证策略，IP，Token，用户名
            }
        });
        /**
         * 性能优化
         */
        config.getSocketConfig().setReuseAddress(true);
        config.getSocketConfig().setSoLinger(0);
        config.getSocketConfig().setTcpNoDelay(true);
        config.getSocketConfig().setTcpKeepAlive(true);

        server = new SocketIOServer(config);
        server.addListeners(new GameEventHandler(server));
        return server;
    }

    @Bean
    public SpringAnnotationScanner springAnnotationScanner(SocketIOServer socketServer) {
        return new SpringAnnotationScanner(socketServer);
    }

//    @Bean
//    public GameEventDispatcher getEventDispatcher(){
//        return new GameEventDispatcher();
//    }

    @PreDestroy
    public void destory() {
        server.stop();
    }
}  